/**
 * Created by yhg97p on 2016/9/27.
 */
define(['app', 'filters/filters'],function(app){
    app.register.controller('accountInfo', function($scope, $http, $state, $sce,$stateParams,$rootScope,$timeout,$modal){
        $scope.loading_show = true;
        $scope.load = {
            _getMajorinfo: function(){  //个人信息
                $http({//请求个人中心-信息详情接口
                    url: app._baseUrl + '/Pub/Account/detail',
                    method: 'POST',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(responed){
                    if(responed.success==true){//已加入企业
                        $scope.loading_show = false;
                        $scope.Majorinfo = responed.data;
                        // $rootScope.file_photo = $scope.app.user.photo;
                        $rootScope.file_photo = $scope.Majorinfo.photo;
                        //默认图片
                        $scope.Majorinfo.photo.length==0||$scope.Majorinfo.photo=='http://localhost/qiyepeixun'?$rootScope.hasimg = true:$rootScope.hasimg = false;
                        $scope.user_name = $scope.Majorinfo.user_name;
                        $scope.real_name = $scope.Majorinfo.real_name;
                        $scope.position = $scope.Majorinfo.position;
                        $rootScope.userName = $scope.Majorinfo.user_name;
                        //性别
                        $scope.Majorinfo.sex==0?$scope.sex_sex = '男':$scope.sex_sex = '女';
                        //生日
                        $scope.bir = angular.toJson($scope.Majorinfo.birthday);//将JSON数据转换成字符串
                        $scope.years = $scope.bir.substring(1,5);//前包后不包
                        $scope.month = $scope.bir.substring(6,8);//月
                        $scope.day = $scope.bir.substring(9,11);//日
                        $scope.birY = $scope.years=='0000'?'':$scope.years;
                        $scope.birM = $scope.month=='00'?'':$scope.month;
                        $scope.birD = $scope.day=='00'?'':$scope.day;

                    }

                });
            },
            init: function(){
                $scope.Majorinfo = [];
                this._getMajorinfo && this._getMajorinfo();
                setTimeout(function () {
                    $scope.initjquery();
                },200)
            }
        };
        $scope.load.init && $scope.load.init();
        $rootScope.file_photo = $scope.app.user.photo;
        $scope.initjquery =function () {
            (function($){
                $.extend({
                    ms_DatePicker: function (options) {
                        var defaults = {
                            YearSelector: "#sel_year",
                            MonthSelector: "#sel_month",
                            DaySelector: "#sel_day",
                            YFirstText: $scope.birY,
                            MFirstText: $scope.birM,
                            DFirstText: $scope.birD,
                            FirstValue: 1
                        };
                        var opts = $.extend({}, defaults, options);
                        var $YearSelector = $(opts.YearSelector);
                        var $MonthSelector = $(opts.MonthSelector);
                        var $DaySelector = $(opts.DaySelector);
                        var YFirstText = opts.YFirstText;
                        var MFirstText = opts.MFirstText;
                        var DFirstText = opts.DFirstText;
                        var FirstValue = opts.FirstValue;

                        // 初始化
                        var Ystr = "<option value=\"" + FirstValue + "\">"+YFirstText+"</option>";
                        var Mstr = "<option value=\"" + FirstValue + "\">"+MFirstText+"</option>";
                        var Dstr = "<option value=\"" + FirstValue + "\">"+DFirstText+"</option>";
                        $YearSelector.html(Ystr);
                        $MonthSelector.html(Mstr);
                        $DaySelector.html(Dstr);

                        // 年份列表
                        var yearNow = new Date().getFullYear();
                        var yearSel = $YearSelector.attr("ng-model");
                        for (var i = yearNow; i >= 1900; i--) {
                            var sed = yearSel==i?"selected":"";
                            var yearStr = "<option value=\"" + i + "\" " + sed+">"+i+"</option>";
                            $YearSelector.append(yearStr);
                        }

                        // 月份列表
                        var monthSel = $MonthSelector.attr("ng-model");
                        for (var i = 1; i <= 12; i++) {
                            var sed = monthSel==i?"selected":"";
                            if(i < 10){
                                var monthStr = "<option value=\"" + i + "\" "+sed+">"+'0'+i+"</option>";
                            }else{
                                var monthStr = "<option value=\"" + i + "\" "+sed+">"+i+"</option>";
                            }
                            $MonthSelector.append(monthStr);
                        }

                        // 日列表(仅当选择了年月)
                        function BuildDay() {
                            if ($YearSelector.val() == 0 || $MonthSelector.val() == 0) {
                                // 未选择年份或者月份
                                $DaySelector.html(Dstr);
                            } else {
                                $DaySelector.html(Dstr);
                                var year = parseInt($YearSelector.val());
                                var month = parseInt($MonthSelector.val());
                                var dayCount = 0;
                                switch (month) {
                                    case 01:
                                    case 03:
                                    case 05:
                                    case 07:
                                    case 08:
                                    case 10:
                                    case 12:
                                        dayCount = 31;
                                        break;
                                    case 04:
                                    case 06:
                                    case 09:
                                    case 11:
                                        dayCount = 30;
                                        break;
                                    case 02:
                                        dayCount = 28;
                                        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                                            dayCount = 29;
                                        }
                                        break;
                                    default:
                                        break;
                                }

                                var daySel = $DaySelector.attr("ng-model");
                                for (var i = 1; i <= dayCount; i++) {
                                    var sed = daySel==i?"selected":"";
                                    if(i < 10){
                                        var dayStr = "<option value=\"" + i + "\" "+sed+">" +'0'+i + "</option>";
                                    }else{
                                        var dayStr = "<option value=\"" + i + "\" "+sed+">"+i+"</option>";
                                    }
                                    $DaySelector.append(dayStr);
                                }
                            }
                        }
                        $MonthSelector.change(function () {
                            BuildDay();
                        });
                        $YearSelector.change(function () {
                            BuildDay();
                        });
                        if($DaySelector.attr("rel")!=""){
                            BuildDay();
                        }
                    }
                });
            })(jQuery);
            $.ms_DatePicker({
                YearSelector: ".sel_year",
                MonthSelector: ".sel_month",
                DaySelector: ".sel_day"
            });

        };
        // 教师图片上传
        $scope.file_img_show = '';
        $scope.$watch('file_img', function(n, o) {
            if (n && n != o && $scope.file_img) {
                var reader = new FileReader();
                reader.onload = function(event) {
                    var txt = event.target.result;
                    $scope.file_img_show = txt;
                    $scope.$apply(function(){

                    })
                };
                reader.readAsDataURL($scope.file_img);
            }
        });

        //给保存一个点击事件
        $scope.save = function(){
            $scope.img = $scope.file_img;
            if($scope.user_name==''){
                $scope.sec = 5;
                var interval = setInterval(function() {//设置定时器
                    if($scope.sec > 0) {
                        $scope.sec--;
                        $scope.tipShow = true;//提示显示
                        $scope.tipmess = '昵称不能为空';//将提示信息绑定
                        $('.black_patch').fadeIn(600);
                    } else {
                        clearInterval(interval);
                        $scope.tipShow = true;
                        $('.black_patch').fadeOut(600);
                    }
                    $scope.$digest();//脏数据检验
                }, 500);
                return false;
            }
            if(!$scope.file_img){
                $scope.img = '';
            }
            if(!$scope.birY||!$scope.birM){
                $scope.birthday = '';
            }else{
                if($scope.birM.length==1){
                    $scope.birM = '0'+$scope.birM;
                }
                $scope.birthday = $scope.birY+'-'+$scope.birM;
            }
            $scope.sex_sex_up = $scope.sex_sex;
            if($scope.sex_sex == '男'){
                $scope.sex_sex_up = '0';
            }
            if($scope.sex_sex == '女'){
                $scope.sex_sex_up = '1';
            }
            var postData = {
                user_name:$scope.user_name,
                sex:$scope.sex_sex_up,
                birthday:$scope.birthday,
                photo:$scope.img,
                position:$scope.position,
                real_name:$scope.real_name
            };
            var promise = postMultipart(app._baseUrl + '/Pub/Account/update', postData);
            function postMultipart(url, data) {
                var fd = new FormData();
                angular.forEach(data, function(val, key) {
                    fd.append(key, val);
                });
                var args = {
                    method: 'POST',
                    url: url,
                    data: fd,
                    headers: {'Content-Type': undefined},
                    transformRequest: angular.identity
                };
                return $http(args).success(function (data) {
                    if(data.success==true){
                        $scope.app.user.photo = $scope.file_img_show;
                        $rootScope.file_photo = $scope.app.user.photo;
                        $scope.load.init && $scope.load.init();

                    }else{

                    }
                    $scope.sec = 5;
                    var interval = setInterval(function() {//设置定时器
                        $scope.number = 1;//退回当前页面
                        if($scope.sec > 0) {
                            $scope.sec--;
                            $scope.tipShow = true;//提示显示
                            $scope.tipmess = data.message;//将提示信息绑定
                            $('.black_patch').fadeIn(600);
                        } else {
                            clearInterval(interval);
                            $scope.tipShow = true;
                            $('.black_patch').fadeOut(600);
                        }
                        $scope.$digest();//脏数据检验
                    }, 500)
                })
            }

        }
        $rootScope.file_photo = $scope.app.user.photo;
        $scope.number = 1;
        //个人中心点击事件
        $scope.perCenter = function(){
            $scope.number = 1;
        }
        //修改密码点击事件
        $scope.changePass = function(){
            $scope.number = 2;
        }
        //给修改按钮一个点击事件
        $scope.modification = function(){
            $scope.password = '';
            $scope.newpassword = '';
            $scope.repassword = '';
            $scope.number = 3;
        }
        //给关闭按钮一个点击事件
        $scope.close = function(){
            $scope.number = 2;
        }
        $scope.mouse = false;
        $scope.tipShow = false;

        //--------------------------------------------------------------弹窗------------------
        //给确定按钮一个点击事件
        $scope.confirm = function(){
            $scope.tipmess = '';
            $scope.mess = '';
            if(!$scope.password||!$scope.newpassword||!$scope.repassword){
                $('.tips').text('密码不能为空').show();
                return false;
            }
            if($scope.newpassword.length<6){//密码长度小于6位
                $('.tips').text('密码长度要求6-12位数，区分大小写').show();
                return false;
            }
            if($scope.newpassword!=$scope.repassword){
                $('.tips').text('两次密码输入不一致').show();
                return false;
            }
            if($scope.newpassword.length>=6&&$scope.newpassword.length<=12){
                $http({//请求个人中心-修改密码接口
                    url: app._baseUrl + 'Pub/Account/changePwd',
                    method: 'POST',
                    data:{
                        password:$scope.password,
                        newpassword:$scope.newpassword,
                        repassword:$scope.repassword
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(res){
                    $scope.sec = 5;
                    var interval = setInterval(function() {//设置定时器
                        $scope.number = 2;//退回当前页面
                        if($scope.sec > 0) {
                            $scope.sec--;
                            $scope.tipShow = true;//提示显示
                            $scope.tipmess = res.message;//将提示信息绑定
                            $('.black_patch').fadeIn(600);
                        } else {
                            clearInterval(interval);
                            $scope.tipShow = true;
                            $('.black_patch').fadeOut(600);
                        }
                        $scope.$digest();//脏数据检验
                    }, 500)
                })
            }

        }
    })
})